/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.date.DateUtil
 *  cn.hutool.core.util.ObjectUtil
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.ComboBox
 *  javafx.scene.control.TextArea
 *  javafx.scene.control.TextField
 *  javafx.scene.layout.AnchorPane
 *  javafx.stage.Stage
 *  javafx.stage.WindowEvent
 *  javafx.util.StringConverter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.BeanUtils
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.salesReceipts;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.gys.common.business.SalesReceiptsUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.data.KeyValue;
import com.gys.common.util.CommonUtil;
import com.gys.common.util.SpringUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.salesReceipts.RecipelDrugsController;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.controller.salesReceipts.SelectRecipelDrugsController;
import com.gys.controller.salesReceipts.TipTopController;
import com.gys.data.example.GetUserOutData;
import com.gys.data.salesReceipts.GetSalesReceiptsTableOutData;
import com.gys.data.salesReceipts.RecipelInfoInputData;
import com.gys.data.salesReceipts.SelectRecipelDrugsNoCheckOutData;
import com.gys.data.salesReceipts.SelectRecipelDrugsOutData;
import com.gys.view.salesReceipts.RecipelInfoInputView;
import de.felixroske.jfxsupport.FXMLController;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.StringConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class RecipelInfoInputController
extends BaseController
implements Initializable {
    private static final Logger log = LoggerFactory.getLogger(RecipelInfoInputController.class);
    @FXML
    private TextField doctor;
    @FXML
    private TextArea symptom;
    @FXML
    private TextField phone;
    @FXML
    private TextArea diacrisis;
    @FXML
    private ComboBox<KeyValue> sex;
    @FXML
    private TextField cardId;
    @FXML
    private TextField caseId;
    @FXML
    private TextField name;
    @FXML
    private TextField hospitalName;
    @FXML
    private TextField office;
    @FXML
    private AnchorPane pane;
    @FXML
    private TextField age;
    @Autowired
    private TipTopController tipTopController;
    @Autowired
    private RecipelDrugsController recipelDrugsController;
    @Autowired
    private SelectRecipelDrugsController selectRecipelDrugsController;
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;
    public int num = 0;
    public boolean flag = false;
    public List<RecipelInfoInputData> recipelInfoInputDataList = new ArrayList<RecipelInfoInputData>();

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        CommonUtil.initIcon(RecipelInfoInputView.class);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.pane)) {
            return;
        }
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                RecipelInfoInputView view = SpringUtil.getBean(RecipelInfoInputView.class);
                Stage stage = (Stage)view.getView().getScene().getWindow();
                stage.setOnCloseRequest((EventHandler)new EventHandler<WindowEvent>(){

                    public void handle(WindowEvent event) {
                        if (!RecipelInfoInputController.this.flag) {
                            RecipelInfoInputController.this.selectRecipelDrugsController.removeSelect();
                        }
                    }
                });
            }
        }, 500L);
        this.tipTopController.initTitle("\u5904\u65b9" + this.num + "\u4fe1\u606f\u5f55\u5165");
        this.flag = false;
        this.name.clear();
        this.sex.setValue(new KeyValue());
        this.doctor.clear();
        this.age.clear();
        this.cardId.clear();
        this.phone.clear();
        this.caseId.clear();
        this.hospitalName.clear();
        this.office.clear();
        this.symptom.clear();
        this.diacrisis.clear();
        ObservableList sexOptions = FXCollections.observableArrayList(new ArrayList());
        sexOptions.add(new KeyValue("\u7537", "1"));
        sexOptions.add(new KeyValue("\u5973", "0"));
        this.sex.setItems(sexOptions);
        this.comboxInit(this.sex);
        if (this.num == 1) {
            this.recipelInfoInputDataList = new ArrayList<RecipelInfoInputData>();
        }
    }

    public void comboxInit(ComboBox box) {
        box.converterProperty().set(new StringConverter<KeyValue>(){

            public String toString(KeyValue object) {
                return object.getName();
            }

            public KeyValue fromString(String string) {
                return null;
            }
        });
    }

    @FXML
    private void sureAction() {
        if (!this.checkData()) {
            return;
        }
        RecipelInfoInputData recipelInfoInputData = new RecipelInfoInputData();
        recipelInfoInputData.setFirstInput(1 == this.num);
        recipelInfoInputData.setDoctor(this.doctor.getText());
        recipelInfoInputData.setSymptom(this.symptom.getText());
        recipelInfoInputData.setPhone(this.phone.getText());
        recipelInfoInputData.setDiacrisis(this.diacrisis.getText());
        recipelInfoInputData.setCardId(this.cardId.getText());
        recipelInfoInputData.setSex(((KeyValue)this.sex.getValue()).getValue());
        recipelInfoInputData.setCaseId(this.caseId.getText());
        recipelInfoInputData.setName(this.name.getText());
        recipelInfoInputData.setHospitalName(this.hospitalName.getText());
        recipelInfoInputData.setOffice(this.office.getText());
        recipelInfoInputData.setAge(this.age.getText());
        if (!this.recipelDrugsController.flag) {
            recipelInfoInputData.setGssrType("1");
        } else {
            recipelInfoInputData.setGssrType("2");
        }
        HttpJson httpJson = this.save(recipelInfoInputData);
        if (!httpJson.isSuccess()) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
            alert.showAndWait();
            return;
        }
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u4fdd\u5b58\u6210\u529f");
        alert.showAndWait();
        this.flag = true;
        this.pane.getScene().getWindow().hide();
        if (this.recipelDrugsController.numZhang == this.num) {
            this.recipelDrugsController.cancelAction();
            this.recipelDrugsController.inputFlag = true;
        }
    }

    public HttpJson save(RecipelInfoInputData recipelInfoInputData) {
        GetUserOutData loginUser = this.getLoginUser();
        recipelInfoInputData.setClientId(loginUser.getClient());
        recipelInfoInputData.setGssrBrId(loginUser.getDepId());
        recipelInfoInputData.setGssrBrName(loginUser.getDepName());
        recipelInfoInputData.setGssrDate(DateUtil.format((Date)DateUtil.date(), (String)"yyyyMMdd"));
        recipelInfoInputData.setGssrTime(DateUtil.format((Date)DateUtil.date(), (String)"HH:mm:ss"));
        recipelInfoInputData.setGssrEmp(loginUser.getUserId());
        recipelInfoInputData.setGssrSaleBrId(loginUser.getDepId());
        recipelInfoInputData.setGssrSaleDate(DateUtil.format((Date)DateUtil.date(), (String)"yyyyMMdd"));
        recipelInfoInputData.setGssrSaleBillNo(this.salesReceiptsMainController.getCaseId().getText());
        recipelInfoInputData.setGssrCheckStatus("0");
        ArrayList<SelectRecipelDrugsOutData> selectRecipelDrugsOutDataList = new ArrayList<SelectRecipelDrugsOutData>();
        if (this.recipelDrugsController.numZhang == 1) {
            for (int i = 0; i < this.salesReceiptsMainController.recipelDrugList.size(); ++i) {
                GetSalesReceiptsTableOutData salesReceiptsTableOutData = this.salesReceiptsMainController.recipelDrugList.get(i);
                SelectRecipelDrugsOutData selectRecipelDrugsOutData = new SelectRecipelDrugsOutData();
                selectRecipelDrugsOutData.setSpecs(salesReceiptsTableOutData.getProSpecs());
                selectRecipelDrugsOutData.setProFactoryName(salesReceiptsTableOutData.getProFactoryName());
                selectRecipelDrugsOutData.setPrePrice(salesReceiptsTableOutData.getPrcAmount());
                selectRecipelDrugsOutData.setNum(salesReceiptsTableOutData.getNum());
                selectRecipelDrugsOutData.setProName(salesReceiptsTableOutData.getProName());
                selectRecipelDrugsOutData.setProCode(salesReceiptsTableOutData.getProCode());
                selectRecipelDrugsOutData.setProUnit(salesReceiptsTableOutData.getProUnit());
                selectRecipelDrugsOutData.setIndexD(salesReceiptsTableOutData.getIndex());
                if (ObjectUtil.isNotEmpty(salesReceiptsTableOutData.getPdAndExpData())) {
                    selectRecipelDrugsOutData.setGssdBatch(salesReceiptsTableOutData.getPdAndExpData().getGssbBatchNo());
                }
                selectRecipelDrugsOutDataList.add(selectRecipelDrugsOutData);
            }
            this.selectRecipelDrugsController.selectDataIndexList.clear();
            this.selectRecipelDrugsController.selectDataList.addAll(selectRecipelDrugsOutDataList);
            this.selectRecipelDrugsController.selectDataIndexList.add(selectRecipelDrugsOutDataList);
            recipelInfoInputData.setRecipelDrugsList(selectRecipelDrugsOutDataList);
        } else {
            recipelInfoInputData.setRecipelDrugsList(this.selectRecipelDrugsController.selectDataList);
        }
        this.recipelInfoInputDataList.add(recipelInfoInputData);
        HashMap<String, Object> param = new HashMap<String, Object>();
        param.putAll(CommonUtil.convertToMap(recipelInfoInputData));
        if (this.recipelDrugsController.numZhang == 1) {
            param.put("recipelDrugsList", selectRecipelDrugsOutDataList);
        } else {
            ArrayList<SelectRecipelDrugsNoCheckOutData> selectDataList = new ArrayList<SelectRecipelDrugsNoCheckOutData>();
            for (int i = 0; i < this.selectRecipelDrugsController.selectDataList.size(); ++i) {
                SelectRecipelDrugsOutData selectRecipelDrugsOutData = this.selectRecipelDrugsController.selectDataList.get(i);
                SelectRecipelDrugsNoCheckOutData selectRecipelDrugsNoCheckOutData = new SelectRecipelDrugsNoCheckOutData();
                BeanUtils.copyProperties(selectRecipelDrugsOutData, selectRecipelDrugsNoCheckOutData);
                selectDataList.add(selectRecipelDrugsNoCheckOutData);
            }
            param.put("recipelDrugsList", selectDataList);
        }
        HttpJson httpJson = SalesReceiptsUtil.saveRecipelInfo(param);
        return httpJson;
    }

    private boolean checkData() {
        String message = "";
        if (ObjectUtil.isEmpty(this.name.getText())) {
            message = "\u59d3\u540d\u4e0d\u80fd\u4e3a\u7a7a";
        } else if (this.name.getText().length() > 10) {
            message = "\u59d3\u540d\u957f\u5ea6\u4e0d\u80fd\u8d85\u8fc710";
        } else if (ObjectUtil.isEmpty(this.sex.getValue()) || ObjectUtil.isEmpty(((KeyValue)this.sex.getValue()).getValue())) {
            message = "\u6027\u522b\u4e0d\u80fd\u4e3a\u7a7a";
        } else if (ObjectUtil.isEmpty(this.age.getText())) {
            message = "\u5e74\u9f84\u4e0d\u80fd\u4e3a\u7a7a";
        } else if (!CommonUtil.positiveInteger(this.age.getText())) {
            message = "\u5e74\u9f84\u8bf7\u8f93\u5165\u6b63\u6574\u6570\u4e14\u957f\u5ea6\u5c0f\u4e8e\u7b49\u4e8e3\u4f4d";
        } else if (this.age.getText().length() > 3) {
            message = "\u5e74\u9f84\u8bf7\u8f93\u5165\u6b63\u6574\u6570\u4e14\u957f\u5ea6\u5c0f\u4e8e\u7b49\u4e8e3\u4f4d";
        } else if (ObjectUtil.isEmpty(this.cardId.getText())) {
            message = "\u8eab\u4efd\u8bc1\u53f7\u4e0d\u80fd\u4e3a\u7a7a";
        } else if (ObjectUtil.isEmpty(this.phone.getText())) {
            message = "\u8bf7\u8f93\u5165\u624b\u673a\u53f7";
        } else if (ObjectUtil.isEmpty(this.caseId.getText())) {
            message = "\u5904\u65b9\u5355\u53f7\u4e0d\u80fd\u4e3a\u7a7a";
        } else if (ObjectUtil.isEmpty(this.hospitalName.getText())) {
            message = "\u533b\u9662\u4e0d\u80fd\u4e3a\u7a7a";
        } else if (ObjectUtil.isEmpty(this.office.getText())) {
            message = "\u79d1\u5ba4\u4e0d\u80fd\u4e3a\u7a7a";
        } else if (ObjectUtil.isEmpty(this.doctor.getText())) {
            message = "\u533b\u751f\u4e0d\u80fd\u4e3a\u7a7a";
        } else if (!CommonUtil.positiveInteger(this.age.getText())) {
            message = "\u5e74\u9f84\u8bf7\u8f93\u5165\u6b63\u6574\u6570\u4e14\u957f\u5ea6\u5c0f\u4e8e\u7b49\u4e8e3\u4f4d";
        } else if (!CommonUtil.isIDNumber(this.cardId.getText())) {
            message = "\u8eab\u4efd\u8bc1\u53f7\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u6216\u683c\u5f0f\u9519\u8bef";
        } else if (!CommonUtil.checkPhone(this.phone.getText())) {
            message = "\u624b\u673a/\u7535\u8bdd\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u6216\u683c\u5f0f\u9519\u8bef";
        }
        if (ObjectUtil.isNotEmpty(message)) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", message);
            alert.showAndWait();
            return false;
        }
        return true;
    }

    @FXML
    private void cancelAction() {
        this.selectRecipelDrugsController.removeSelect();
        this.pane.getScene().getWindow().hide();
    }
}

